import { createRouter, createWebHistory } from "vue-router";
import { RouteRecordRaw } from "vue-router";
import Store, { initState, resetState } from '@/store';
import backRoutes from "./back";
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',//登录页面
        redirect: '/login',
    },
    {
        path: '/login',
        name: "r_login",
        component: () => import("@/pages/Login.vue")
    },
    ...backRoutes,
    {
        path: '/:pathMatch(.*)*',//匹配所有路径
        component: () => import("@/pages/NoFound.vue")
    },
    {
        path: '/no-found',
        component: () => import("@/pages/NoFound.vue"),//404
        name: "404"
    },

];
const Router = createRouter({
    history: createWebHistory('/'),
    routes
});
Router.beforeEach((to, from, next) => {
    if (to.path.includes("/login")) {
        window.sessionStorage.clear();
        resetState();
        next();
    }
    else {
        const rawData = toRaw(Store.state.value);
        let str = rawData.user.user.token;
        if (!str) {
            window.sessionStorage.clear();
            ElMessage.error("Token过期,请重新登录");
            return next('/login');
        }
        next();
    }

});
export { Router };
